package cn.tx.controller;


import cn.tx.model.Classes;
import cn.tx.model.Clazz;
import cn.tx.model.Grade;
import cn.tx.page.Page;
import cn.tx.service.ClassesService;
import cn.tx.service.GradeService;
import cn.tx.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(path = "/classes")
public class ClassesController {

    @Autowired
    private ClassesService classesService;


    /*首页展示*/
    @RequestMapping(path = "/list")
    public ModelAndView list(ModelAndView modelAndView) {
        modelAndView.setViewName("classes/classes_list");
        return modelAndView;
    }


    /*展示列表*/
    @RequestMapping(path = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getList(
            @RequestParam(value="cName",required=false,defaultValue="") String cName,
            Page page
    ) {
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> querymap = new HashMap<String,Object>();
        querymap.put("cName","%"+cName+"%");
        querymap.put("offset", page.getOffset());
        querymap.put("pageSize", page.getRows());
        map.put("rows",classesService.findList(querymap));
        map.put("total",classesService.findTotal(querymap));
        return map;
    }



    /*添加班级*/
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Classes classes) {    //**add(Grade grade)
        Map<String, String> map = new HashMap<>();
        if (classes == null) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        if (StringUtils.isEmpty(classes.getcName())) {
            map.put("type", "error");
            map.put("msg", "年级名不能为空");
            return map;
        }
//
//        if (StringUtils.isEmpty(classes.getRemark())) {
//            map.put("type", "error");
//            map.put("msg", "备注不能为空");
//            return map;
//        }
        if (classesService.addclasses(classes) <= 0) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功");
        return map;
    }

    /*编辑*/
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Classes classes) {
        Map<String, String> map = new HashMap<>();
//        Clazz userName = classesService.findByUserName(classes.getName());
//        if (userName!=null){
//            if (clazz.getId()!=userName.getId()){
//                map.put("type", "error");
//                map.put("msg", "id不匹配");
//                return map;
//            }
//        }
//        if (clazz.getGradeId()==null){
//            map.put("type", "error");
//            map.put("msg", "年级不能为空");
//            return map;
//        }
        System.out.println(classes);

        if (classesService.UpdateGradeById(classes)<= 0) {
            map.put("type", "error");
            map.put("msg", "修改失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "修改成功");
        return map;
    }



        /*删除班级*/
        @RequestMapping(path = "/delete", method = RequestMethod.POST)
        @ResponseBody
        public Map<String, String> delete(
                @RequestParam(value = "ids[]",required = true) Long[] ids
        ) {
            Map<String, String> map = new HashMap<>();
            if (ids==null||ids.length==0){
                map.put("type", "error");
                map.put("msg", "请至少选择一条数据");
                return map;
            }
            try {
                if(classesService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                    map.put("type", "error");
                    map.put("msg", "删除失败！");
                    return map;
                }
            } catch (Exception e) {
                map.put("type", "error");
                map.put("msg", "该班级下存在信息，请勿冲动！");
                return map;
            }
            map.put("type", "success");
            map.put("msg", "删除成功");
            return map;
        }


}
